<?php
	

		class baseAction extends Action {
			
			protected $userInfo = null; //用户信息
			protected $rank = false;
			protected  function _initialize(){ //初始化action,用于判断用户登录与否？
				$this->userInfo = Session::get("userInfo");
				if(empty($this->userInfo)){
					$this->redirect("login","login");
				}
				$this->assign("userInfo",$this->userInfo);
				//私信
				$message = D("message")->count("userid=".$this->userInfo->id." and isread=0","id");
				if($message>0){
					$this->assign("messagecount",$message);
				}
			}
			
			/**
			 * 添加Feed
			 *
			 * @param unknown_type $userId
			 * @param unknown_type $module
			 * @param unknown_type $content
			 * @param unknown_type $moduleId
			 * @param unknown_type $requestid
			 * @param unknown_type $count
			 */
			protected  function addFeed($userId,$module,$content,$moduleId=0,$requestid=0,$count=1){
				$feedDao = D("feed");
				$feedDao->userid = $userId;
				$feedDao->module = $module;
				$feedDao->moduleid = $moduleId;
				$feedDao->regtime = time();
				$feedDao->content = $content;
				$feedDao->requestid = $requestid;
				if("friend" == $module || "addphoto" == $module){
					$record = $feedDao->find("userid = ".$userId." and module='".$module."' and moduleid=".$moduleId." and regtime > ($feedDao->regtime - 86400)");
					if($record){
						$_content = $feedDao->content.",".$content;
						$feedDao->content = $_content;
						$feedDao->count = '(count+'.$count.')';
						$feedDao->regtime = time();
						$feedDao->save();
					}else{
						$id = $feedDao->add();
					}
				}else{
					$id = $feedDao->add();
				}
				if($id>0){
					$userCacheDao = D("cache");
					$userCacheDao->setField("feedCount","(feedCount+1)","userid=".$userId);
					$userData = $userCacheDao->find("userid=".$userId);
					$path = getCachePath(APP_PATH."/UserCache",$userId);
					F("userData",$userData,$path);
					$feedList = $feedDao->findAll("userid=".$userId,"*","id desc",10);
					F("feedList",$feedList,$path);//cache最新十条
				}
			}
			
			/**
			 * 删除feed
			 *
			 * @param unknown_type $id
			 * @param unknown_type $userId
			 * @param unknown_type $module
			 * @param unknown_type $moduleid
			 * @param unknown_type $requestid
			 */
			protected function delFeed($id,$userId,$module="feed",$moduleid=0,$requestid=0){
				$feedDao = D("feed");
				if($moduleid == 0 ){
					$feedDao->delete($id);
				}else {
					$where = "";
					if($moduleid != 0){
						$where .= " and moduleid=".$moduleid;
					}
					if($requestid != 0){
						$where .= " and requestid=".$requestid;
					}
					$feedDao->delete("module='".$module."'".$where);
				}
				$path = getCachePath(APP_PATH."/UserCache",$userId);
				$userCacheDao = D("cache");
				$userCacheDao->setField("feedCount","(feedCount-1)","userid=".$userId);
				$userData = $userCacheDao->find("userid=".$userId);
				F("userData",$userData,$path);
				$feedList = $feedDao->findAll("userid=".$userId,"*","id desc",10);
				F("feedList",$feedList,$path);//cache最新十条
			}
			
			/**
			 * 添加通知
			 *
			 * @param unknown_type $userid
			 * @param unknown_type $myid
			 * @param unknown_type $module
			 * @param unknown_type $moduleid
			 * @param unknown_type $revertid
			 */
			protected function addNotify($userid,$myid,$module,$moduleid,$revertid=0){
				$dao = D("notify");
				$dao->userid    = $userid;
				$dao->postid    = $myid;
				$dao->module 	= $module;
				$dao->moduleid	= $moduleid;
				$dao->revertid	= $revertid;
				$dao->regtime 	= time();
				$dao->add();
			}
		}
?>